package com.zone.vehicle.superapp.repository;

import android.content.Context;

import com.saicmotor.pay.db.PayDaoUtilsStore;
import com.saicmotor.pay.db.entity.Pay;
import com.zone.lib.download.db.entity.Download;
import com.zone.lib.download.enums.AppStatusEnum;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk.module.appstore.base.StoreUtils;
import com.zone.sdk.module.appstore.entity.AppDetailEntity;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.sdk.module.appstore.entity.ResourceItem;
import com.zone.vehicle.sdk.module.superapp.entity.AppUpdateEntity;
import com.zone.vehicle.sdk.module.superapp.response.AppPushResponse;
import com.zone.vehicle.superapp.bean.AppBean;
import com.zone.vehicle.superapp.datasource.AppManagerDataSource;
import com.zone.lib.download.enums.AppTypeEnum;
import com.zone.vehicle.superapp.utils.AppUsePermission;
import com.zone.vehicle.superapp.utils.LoginUtil;

import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class AppManagerRepository {

    private static volatile AppManagerRepository instance;

    private AppManagerDataSource dataSource;


    // private constructor : singleton access
    private AppManagerRepository(AppManagerDataSource dataSource) {
        this.dataSource = dataSource;
    }

    public static AppManagerRepository getInstance(AppManagerDataSource dataSource) {
        if (instance == null) {
            instance = new AppManagerRepository(dataSource);
        }
        return instance;
    }

    public void getUpdateApp(ResultCallBack<List<AppBean>> callBack){
        List<Download> downloadAppList = dataSource.getLocalApps(AppTypeEnum.APP, AppStatusEnum.INSTALL_SUCCESS);
        List<Download> downloadLightList = dataSource.getLocalApps(AppTypeEnum.APP_LIGHT, AppStatusEnum.LIGHT_INSTALL_COMPLETE);

    }

    public List<AppBean> getActivityApps(AppTypeEnum type){
        return fillAppBeans(dataSource.getActivityApps(type));
    }

    public List<AppBean> getDownloadApps(AppTypeEnum type){
        return fillAppBeans(dataSource.getDownloadApps(type));
    }

    public List<AppBean> getInstallApps(AppTypeEnum type){
        return fillAppBeans(dataSource.getInstallApps(type));
    }

    @NotNull
    private List<AppBean> fillAppBeans(List<Download> downloads) {
        List<AppBean> appBeans = new ArrayList<>();
        List<String> appIds = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(downloads)){
            for (int i = 0; i < downloads.size(); i++) {
                Download download = downloads.get(i);
                if (!AppUsePermission.hasPermission(download)){
                    continue;
                }
                appIds.add(download.getCode());
                AppBean appBean = fillLocalAppBean(download);
                appBeans.add(appBean);
            }
        }
        return appBeans;
    }

    public void appList(List<String> codeList, ResultCallBack<Result<List<AppEntity>>> callBack){
        dataSource.appList(codeList, callBack);
    }

    @NotNull
    private List<AppBean> buildAppBeans(List<Download> downloads, List<AppEntity> appEntities) {
        List<AppBean> appBeans = new ArrayList<>();
        for (int i = 0; i < downloads.size(); i++) {
            AppBean appBean = new AppBean();
            Download download = downloads.get(i);
            for (int j = 0; j < appEntities.size(); j++) {
                AppEntity appEntity = appEntities.get(j);
                if (download.getCode().equals(appEntity.getCode())){
                    appBean.setDownload(download);
                    appBean.buildFromEntry(appEntity);
                    appBeans.add(appBean);
                }
            }
        }
        return appBeans;
    }

    @NotNull
    private List<AppBean> fillLocalAppBeans(List<Download> downloads) {
        List<AppBean> appBeanList = new ArrayList<>();
        for (int i = 0; i < downloads.size(); i++) {
            Download download = downloads.get(i);
            if (!AppUsePermission.hasPermission(download)){
                continue;
            }
            appBeanList.add(fillLocalAppBean(download));
        }
        return appBeanList;
    }



    @NotNull
    private AppBean fillLocalAppBean(Download download) {
        AppBean appBean = new AppBean();
        appBean.setCode(download.getCode());
        appBean.setPackageName(download.getPackageName());
        appBean.setName(download.getName());
        appBean.setAppType(download.getAppType());
        appBean.setIconUrl(download.getIcon());
//        appBean.setPreview(download.getPreview());
//        appBean.setPreviewType(download.getPreviewType());
        ResourceItem item = new ResourceItem();
        item.setResourceType(download.getPreviewType());
        item.setResourceUrl(download.getPreview());
        item.setResourceId(StoreUtils.RESOURCE_ID_PREVIEW);
        List<ResourceItem> resourceItemList = new LinkedList<>();
        resourceItemList.add(item);
        appBean.setResourceList(resourceItemList);
        appBean.setVersion(download.getVersion());
        appBean.setDownload(download);
        return appBean;
    }

    public List<AppBean> getInstalledApp(AppTypeEnum type){
        List<AppBean> appEntityList = new ArrayList<>();
        if (type == null){
            appEntityList.addAll(getAppBean(AppTypeEnum.APP, AppStatusEnum.INSTALL_SUCCESS));
            appEntityList.addAll(getAppBean(AppTypeEnum.APP, AppStatusEnum.UPDATE));
            appEntityList.addAll(getAppBean(AppTypeEnum.APP_LIGHT, AppStatusEnum.LIGHT_INSTALL_COMPLETE));
        }else if (type == AppTypeEnum.APP){
            appEntityList.addAll(getAppBean(AppTypeEnum.APP, AppStatusEnum.INSTALL_SUCCESS));
            appEntityList.addAll(getAppBean(AppTypeEnum.APP, AppStatusEnum.UPDATE));
        }else if (type == AppTypeEnum.APP_LIGHT){
            appEntityList.addAll(getAppBean(AppTypeEnum.APP_LIGHT, AppStatusEnum.LIGHT_INSTALL_COMPLETE));
        }
        return appEntityList;
    }

    public List<AppBean> getAppBean(AppTypeEnum type, AppStatusEnum appStatusEnum){
        List<AppBean> appBeanList = new ArrayList<>();
        List<Download> downloadApps = dataSource.getLocalApps(type, appStatusEnum);
        if (ObjectUtils.isNotEmpty(downloadApps)){
            appBeanList.addAll(fillLocalAppBeans(downloadApps));
        }
        return appBeanList;
    }

    public List<AppBean> getLocalInstalledApps(AppTypeEnum type){
        List<AppBean> appBeanList = new ArrayList<>();
        List<Download> downloadApps = dataSource.getLocalInstalledApps(type);
        if (ObjectUtils.isNotEmpty(downloadApps)){
            appBeanList.addAll(fillLocalAppBeans(downloadApps));
        }
        return appBeanList;
    }

    public void downLoadApp(Context context, int action, AppDetailEntity appEntity){
        dataSource.downLoadApp(context, action, appEntity);
    }

    public void updateApp(Context context, int action, AppBean appBean){
        dataSource.updateApp(context, action, appBean);
    }

    public void appletPush(Context context, AppDetailEntity appEntity, AppStatusEnum statusEnum, ResultCallBack<Result<AppPushResponse>> callback){
        dataSource.appletPush(context, appEntity, statusEnum, callback);
    }

    public void updateList(Context context, String appCode, AppTypeEnum appTypeEnum, ResultCallBack<Result<List<AppUpdateEntity>>> callback){
        dataSource.updateList(context, appCode, appTypeEnum, callback);
    }

    public void delete(Context context, AppDetailEntity appEntity){
        dataSource.delete(context, appEntity);
    }

    public void install(Context context, AppDetailEntity appEntity, AppStatusEnum statusEnum){
        dataSource.install(context, appEntity, statusEnum);
    }

    public void getAppDetail(Context context, String code, ResultCallBack<Result<AppDetailEntity>> callback) {
        dataSource.getAppDetail(context, code, callback);
    }
}